package com.example.waterorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.waterorder.dto.BuildingMap;
import com.example.waterorder.entity.Building;
import com.example.waterorder.mapper.BuildingMapper;
import com.example.waterorder.service.BuildingService;
import com.example.waterorder.service.OrderService;
import com.example.waterorder.tools.DistanceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Resource
    private BuildingMapper buildingMapper;
    @Autowired
    private OrderService orderService;

    /**
     * 查询所有building，渲染到学生注册宿舍楼选择框
     * @return
     */
    @Override
    public List<Building> selectAllBuildings() {
        return buildingMapper.selectList(null);
    }

    @Override
    public List<BuildingMap> getAllBuildingMap(String workerId,String lng,String lat) {
        List<BuildingMap> buildingMapList = new ArrayList<>();
        List<Building> buildingList = buildingMapper.selectList(null);
        for (Building building : buildingList) {
            Integer quantity = orderService.selectQuantity(workerId
                    ,building.getName(),1);
            if (quantity!=0&&quantity!=null)
            {
                BuildingMap buildingMap = new BuildingMap();
                buildingMap.setQuantity(quantity);
                buildingMap.setId(building.getBid());
                buildingMap.setName(building.getName());
                buildingMap.setLongitude(building.getLng());
                buildingMap.setLatitude(building.getLat());
                buildingMap.setDistance(
                        DistanceUtils.getDistanceStr(lng,lat,building.getLng(),building.getLat()));
                buildingMapList.add(buildingMap);
            }
        }
        return buildingMapList;
    }

    @Override
    public List<String> selectAllBuildingsName() {
        List<Building> buildings = buildingMapper.selectList(new QueryWrapper<Building>(null).orderByAsc( "name"));
        ArrayList<String> names = new ArrayList<>();
        for (Building building : buildings) {
            names.add(building.getName());
        }
        return names;
    }
}
